package com.daijie.demo

import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

fun <T> Observable<T>.from_computation(): Observable<T> = this.subscribeOn(Schedulers.computation())

fun <T> Observable<T>.from_io(): Observable<T> = this.subscribeOn(Schedulers.io())

fun <T> Observable<T>.from_new(): Observable<T> = this.subscribeOn(Schedulers.newThread())

fun <T> Observable<T>.from_ui(): Observable<T> = this.subscribeOn(AndroidSchedulers.mainThread())

fun <T> Observable<T>.to_ui(): Observable<T> = this.observeOn(AndroidSchedulers.mainThread())

fun <T> Observable<T>.to_io(): Observable<T> = this.observeOn(Schedulers.io())

fun <T> Observable<T>.to_computation(): Observable<T> = this.observeOn(Schedulers.computation())

fun <T> Observable<T>.to_new(): Observable<T> = this.observeOn(Schedulers.newThread())

fun <T> Single<T>.from_io(): Single<T> = this.subscribeOn(Schedulers.io())
fun <T> Single<T>.to_ui(): Single<T> = this.observeOn(AndroidSchedulers.mainThread())

fun <T> Maybe<T>.from_io(): Maybe<T> = this.subscribeOn(Schedulers.io())
fun <T> Maybe<T>.to_ui(): Maybe<T> = this.observeOn(AndroidSchedulers.mainThread())

fun <T> Observable<T>.runRxUIThread(call: (data: T) -> Unit) =
        to_ui().subscribe({
            call.invoke(it)
        }, {
        })

